package com.xizi.秋招笔试;

/**
 * @author xizizzz
 * @description: TODO
 * @date 2021-8-11下午 07:45
 */
public class K {

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5};
        long minimum = minimum(a);
        System.out.println(minimum);
    }

    public static long minimum(int[] a) {
        int[] sum = new int[a.length];
        sum[0] = a[0];
        for (int i = 1; i < a.length; i++) {
            sum[i] = sum[i - 1] + a[i];
        }
        int sum0 = sum[a.length - 1];
        int res = Integer.MAX_VALUE;
        for (int i = 0; i < a.length; i++) {
            for (int j = i; j < a.length; j++) {
                int sum1 = 0;
                if (i == 0) {
                    sum1 = sum[j];
                } else {
                    sum1 = sum[j] - sum[i - 1];
                }
                int sum2 = sum0 - sum1;
                res = Math.min(res, Math.abs(sum1 - sum2));
            }
        }
        return res;
    }
}
